package com.termine.jsf;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.termine.bean.Bean;
import com.termine.entity.Location;
import com.termine.entity.PersonalData;

@ManagedBean
@RequestScoped
public class RegisterJSF extends Bean implements Serializable {

	private static final long serialVersionUID = -3165683647937755679L;

	private PersonalData personalData;
	private Location location;
	private String confirmPassword;
	private boolean registerAsProvider;

	/*
	 * Init
	 */
	@PostConstruct
	public void init() {
		location = new Location();
		personalData = new PersonalData();
		confirmPassword = "";
		registerAsProvider = false;
	}
	
	/*
	 * Jsf Action Methods
	 */
	public String performRegister() {
		if (personalData.getPassword().equals(confirmPassword)) {
			if (registerAsProvider) {
				if (getPersonalDataManager().addProvider(personalData, location)) {
					getLoginJSF().setWhoAmI("PROVIDER");
					return "PROVIDER";
				}
			} else {
				if (getPersonalDataManager().addClient(personalData, location)) {
					getLoginJSF().setWhoAmI("CLIENT");
					return "CLIENT";
				}
			}
		}
		return "ERROR";
	}

	/*
	 * Getters & Setters
	 */
	public PersonalData getPersonalData() {
		return personalData;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public void setPersonalData(PersonalData personalData) {
		this.personalData = personalData;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public boolean isRegisterAsProvider() {
		return registerAsProvider;
	}

	public void setRegisterAsProvider(boolean registerAsProvider) {
		this.registerAsProvider = registerAsProvider;
	}

}
